package com.wink_172.library.view

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.wink_172.library.R

/**
 * 可在外部修改部分样式 使用方法如下
 * TextView   content_view= ((ActionBarView) binding.actionBar).findViewById(R.id.content_view);
 * content_view.setTextColor();
 */
open class ActionBarView : RelativeLayout, View.OnClickListener {
    open lateinit var left_icon: ImageView
    open lateinit var right_icon: ImageView
    open lateinit var left_text: TextView
    open lateinit var content: TextView
    open lateinit var right_text: TextView
    open lateinit var line: View
    open lateinit var left_contain: RelativeLayout
    fun setCallback(callback: ICallback?) {
        mCallback = callback
    }

    var mCallback: ICallback? = null

    interface ICallback {
        fun onSendEvent(event: Int, content: String?)
    }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {}

    override fun onFinishInflate() {
        super.onFinishInflate()
        initView()
    }

    override fun onClick(v: View) {
        mCallback?.let {
        if (v.id == R.id.ac_left_contain) {
            it.onSendEvent(LEFT_BTN, null)
        } else if (v.id == R.id.right_icon || v.id == R.id.ac_right_text) {
            it.onSendEvent(RIGHT_BTN, null)
        } }
    }

    private fun initView() {
        left_text = findViewById(R.id.ac_left_text)
        left_contain = findViewById(R.id.ac_left_contain)
        left_icon = findViewById(R.id.ac_left_icon)
        content = findViewById(R.id.ac_content_view)
        right_text = findViewById(R.id.ac_right_text)
        line = findViewById(R.id.ac_line_view)
        right_icon = findViewById(R.id.ac_right_icon)
        left_contain?.setOnClickListener(this)
        right_text?.setOnClickListener(this)
        right_icon?.setOnClickListener(this)

//        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
//        layoutParams.topMargin = ConmonUtils.getStatusBarHeight(getContext());
    }

    fun initialized(type: Int, flags: Int, vararg args: Any?) {
//        RelativeLayout loadView = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.action_bar_view, null);
//        removeAllViews();
//        addView(loadView);
    }

    fun updateAllContent(leftIcon: Int, text: String?, rightText: String?) {
        left_icon!!.setImageResource(leftIcon)
        content!!.text = text
        right_text!!.text = rightText
    }

    fun updateAllContent2(leftIcon: Int, text: String?, rightText: String?, leftText: String?) {
        left_icon!!.setImageResource(leftIcon)
        content!!.text = text
        right_text!!.text = rightText
        left_text!!.text = leftText
    }

    fun updateAllContent3(leftIcon: Int, text: String?, rightIcon: Int) {
        left_icon!!.setImageResource(leftIcon)
        content!!.text = text
        right_text!!.text = ""
        right_icon!!.setImageResource(rightIcon)
    }

    fun updateAllContent4(leftIcon: Int, text: String?, rightIcon: Int, centerIcon: Int) {
        left_icon!!.setImageResource(leftIcon)
        content!!.text = text
        val drawable = context.resources.getDrawable(centerIcon)
        // 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        content!!.setCompoundDrawables(drawable, null, null, null)
        right_text!!.text = ""
        right_icon!!.setImageResource(rightIcon)
    }

    fun updateAllContent5(leftIcon: Int, text: String?, rightRes: Drawable?, rightText: String?) {
        left_icon!!.setImageResource(leftIcon)
        content!!.text = text
        right_text!!.text = rightText
        right_text!!.background = rightRes
        val layoutParams = LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
        layoutParams.addRule(CENTER_VERTICAL)
        layoutParams.addRule(ALIGN_PARENT_RIGHT)
        layoutParams.rightMargin = 10
        right_text!!.layoutParams = layoutParams
        right_text!!.setPadding(24, 12, 24, 12)
    }

    fun setLineEnable(enable: Boolean) {
        line!!.visibility = if (enable) VISIBLE else INVISIBLE
        //        content_view.setTextColor(getResources().getColor(R.color.white));
    }

    fun updateContent(text: String?) {
        content!!.text = text
    }

    companion object {
        const val LEFT_BTN = 0
        const val RIGHT_BTN = 1
    }
}